// ActionScript file

package utils
{
	import flash.utils.getTimer;

	public class GameState
	{
		static private var _instance:GameState;
		static public function get instance():GameState
		{
			if(!_instance)
			{
				new GameState;
			}
			
			return _instance;
		}
		
		public function GameState():void
		{
			if(_instance)
			{
				throw new Error("singleton error");
			}
			
			_instance = this;
			
			init();
		}
		
		private var _lastDrawTime:uint;
		private var _drawElapsed:Number;			//s
		private var _curDrawCnt:uint;
		private var _fps:uint;
		private var _totalDrawtime:Number;
		
		public function init():void
		{
			_lastDrawTime = 0;
			_drawElapsed = 0;
			_curDrawCnt = 0;
			_fps = 0;
			_totalDrawtime = 0;
		}
		
		public function freshFrame():void
		{
			var cur:uint = getTimer();
			_drawElapsed = (cur - _lastDrawTime) / 1000;
			_totalDrawtime += _drawElapsed;
			++_curDrawCnt;
			
			if(_totalDrawtime > 0.5)
			{
				_fps = int(_curDrawCnt / _totalDrawtime);
				_curDrawCnt = 0;
				_totalDrawtime = 0;
			}
			
			_lastDrawTime = cur;
		}
		
		public function get elapsed():Number
		{
			return _drawElapsed;
		}
		
		public function get fps():uint
		{
			return _fps;
		}
	}
}